package com.cloud.service.Impl;

import com.cloud.dao.ManagerNoticeDao;
import com.cloud.domain.LoginUser;
import com.cloud.myInterface.Factory;
import com.cloud.pojo.ManagerNoticeForManager;
import com.cloud.pojo.ManagerNoticeForManagerVue;
import com.cloud.service.ManagerNoticeService;
import com.cloud.pojo.ManagerNotice;
import com.cloud.tools.CommonResult;
import com.cloud.tools.CommonResultFactory;
import com.cloud.utils.MyTime;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： linJianHong
 * @dateTime： 14:42 2022/4/24
 * @Description:
 * @ClassName:
 */
@Service
public class ManagerNoticeServiceImpl implements ManagerNoticeService {
    @Resource
    ManagerNoticeDao managerNoticeDao;

    private final Factory resultFactory = new CommonResultFactory();

    @Override
    public CommonResult create(Map<String, Object> map) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String managerId = loginUser.getUser().getUserId();
        String notice = (String)map.get("notice");
        String title = (String)map.get("title");

        ManagerNotice managerNotice = new ManagerNotice();
        managerNotice.setManagerId(managerId);
        managerNotice.setNotice(notice);
        managerNotice.setPublishTime(MyTime.getCurrTimeAsDate());
        managerNotice.setTitle(title);

        int res = managerNoticeDao.create(managerNotice);
        if(res == 1){
            return resultFactory.getResultInstance(200,"公告创建成功",1);
        }
        return resultFactory.getResultInstance(200,"公告创建失败，请稍后重试",0);
    }

    @Override
    public CommonResult update(Map<String, Object> map) {
        int id = (int)map.get("id");
        String notice = (String)map.get("notice");
        String title = (String)map.get("title");
        int res = managerNoticeDao.update(id, title, notice, MyTime.getCurrTimeAsDate());
        if(res == 1){
            return resultFactory.getResultInstance(200,"公告修改成功",1);
        }
        return resultFactory.getResultInstance(200,"公告修改失败，请稍后重试",0);
    }

    @Override
    public CommonResult delete(Map<String, Object> map) {
        int id = (int)map.get("id");
        int res = managerNoticeDao.delete(id);

        if(res == 1){
            return resultFactory.getResultInstance(200,"公告删除成功",1);
        }
        return resultFactory.getResultInstance(200,"公告删除失败，请稍后重试",0);
    }

    @Override
    public CommonResult retrieve() {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String managerId = loginUser.getUser().getUserId();

        List<ManagerNoticeForManager> res1 = managerNoticeDao.retrieve(managerId);
        List<ManagerNoticeForManagerVue> res2 = new ArrayList<>();
        for(ManagerNoticeForManager item: res1){
            ManagerNoticeForManagerVue managerNoticeForManagerVue = new ManagerNoticeForManagerVue();
            managerNoticeForManagerVue.setId(item.getId());
            managerNoticeForManagerVue.setNotice(item.getNotice());
            managerNoticeForManagerVue.setPublishTime(MyTime.dateTime2String(item.getPublishTime()));
            managerNoticeForManagerVue.setTitle(item.getTitle());
            res2.add(managerNoticeForManagerVue);
        }
        if(res2.size() >= 1){
            return resultFactory.getResultInstance(200,"公告查看成功",res2);
        }
        return resultFactory.getResultInstance(200,"暂无公告",0);
    }
}
